//
// Created by Asus on 2025/6/2.
//

#include <iostream>
#include <set>
using namespace std;

void printSet(set<int> set) {
    for (auto it = set.begin(); it != set.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}

void demo01() {
    set<int> s1;
    s1.insert(1);
    s1.insert(2);
    s1.insert(3);
    printSet(s1);

    set<int> s2(s1);
    printSet(s2);

    set<int> s3;
    s3 = s2;
    printSet(s3);
}

void demo02() {
    set<int> s1;

    s1.insert(10);
    s1.insert(30);
    s1.insert(20);
    s1.insert(40);

    if (s1.empty()) {
        cout << "s1为空" << endl;
    } else {
        cout << "s1不为空" << endl;
        cout << "s1的大小为： " << s1.size() << endl;
    }

    set<int> s2;

    s2.insert(100);
    s2.insert(300);
    s2.insert(200);
    s2.insert(400);

    cout << "交换前" << endl;
    printSet(s1);
    printSet(s2);
    cout << endl;

    cout << "交换后" << endl;
    s1.swap(s2);
    printSet(s1);
    printSet(s2);
}

void demo03() {
    set<int> s1;
    //插入
    s1.insert(10);
    s1.insert(30);
    s1.insert(20);
    s1.insert(40);
    printSet(s1);
    //删除
    s1.erase(s1.begin());
    printSet(s1);
    s1.erase(30);
    printSet(s1);
    //清空
    //s1.erase(s1.begin(), s1.end());
    s1.clear();
    printSet(s1);
}

void demo04() {
    set<int> s1;
    s1.insert(10);
    s1.insert(30);
    s1.insert(20);

    set<int>::iterator pos = s1.find(20);
    cout << "pos:" << *pos << endl;
    cout << "s1.end():" << *s1.end() << endl;
    cout << *s1.begin() << endl;
    if (pos != s1.end()) {
        cout << "找到了元素 ： " << *pos << endl;
    } else {
        cout << "未找到元素" << endl;
    }
    //统计
    int num = s1.count(30);
    cout << "num = " << num << endl;
}

void demo05() {
    set<int> s;
    pair<set<int>::iterator, bool> ret = s.insert(10);
    if (ret.second == true) {
        cout << "第一次插入成功!" << endl;
    } else {
        cout << "第一次插入失败!" << endl;
    }

    ret = s.insert(10);
    if (ret.second) {
        cout << "第二次插入成功!" << endl;
    } else {
        cout << "第二次插入失败!" << endl;
    }
    //multiset
    multiset<int> ms;
    ms.insert(10);
    ms.insert(10);

    for (multiset<int>::iterator it = ms.begin(); it != ms.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}

void demo06() {
    pair<string, int> p(string("Tom"), 20);
    cout << "姓名： " << p.first << " 年龄： " << p.second << endl;
    pair<string, int> p2 = make_pair("Tom", 20);
    cout << "姓名： " << p2.first << " 年龄： " << p2.second << endl;
}

class MyCompare
{
public:
    bool operator()(int v1, int v2) const {
        return v1 > v2;
    }
};

void demo07() {
    set<int> s1;
    s1.insert(10);
    s1.insert(40);
    s1.insert(20);
    s1.insert(30);
    s1.insert(50);

    //默认从小到大
    for (set<int>::iterator it = s1.begin(); it != s1.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;

    //指定排序规则
    set<int,MyCompare> s2;
    s2.insert(10);
    s2.insert(40);
    s2.insert(20);
    s2.insert(30);
    s2.insert(50);

    for (set<int, MyCompare>::iterator it = s2.begin(); it != s2.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}

class Person
{
public:
    Person(string name, int age)
    {
        this->m_Name = name;
        this->m_Age = age;
    }

    string m_Name;
    int m_Age;

};
class comparePerson
{
public:
    bool operator()(const Person& p1, const Person &p2) const
    {
        //按照年龄进行排序  降序
        return p1.m_Age > p2.m_Age;
    }
};

void demo08() {
    set<Person, comparePerson> s;

    Person p1("刘备", 23);
    Person p2("关羽", 27);
    Person p3("张飞", 25);
    Person p4("赵云", 21);

    s.insert(p1);
    s.insert(p2);
    s.insert(p3);
    s.insert(p4);

    for (set<Person, comparePerson>::iterator it = s.begin(); it != s.end(); it++)
    {
        cout << "姓名： " << it->m_Name << " 年龄： " << it->m_Age << endl;
    }
}

int main(int argc, char *argv[]) {
    // demo01();
    // demo02();
    // demo03();
    // demo04();
    // demo05();
    // demo06();
    // demo07();
    demo08();
    return 0;
}
